﻿package  
{
	import Common.GameCreatureAction;
	import Common.GameDefine;
	import Common.MapDirection;
	import Common.RenderManager;
	import Common.RoleConfig;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import logic.GameCreatureData;
	
	import map.MapGrid;
	import map.MapPos;
	import map.PathFinder;
	
	import ui.MapUIHandler;
	import Common.GameCommonAction;
	
	/**
	 * ...
	 * @author fox
	 */
	public class GameCreature extends RenderObject
	{
		public var data:GameCreatureData = null;
		
		public var mainPlayer:Boolean = false;
		
		public var moveHandler:GameCreatureMove = null;
		//public var moveBuffer:Array = null;
		private var actionHandler:GameCreatureAction = null;
		private var actionArray:Array = null;
		
		public var state:int = 0;
		
		public var team:GameTeam = null;
		
		
		
		////////////////////////////////////////////////////////////////////////////////////
		// data
		
		public function CheckState( s:int ):Boolean
		{
			return state == s;
		}
		
		public function get Name():String
		{
			return data.Name;
		}
		
		public function get ID():int
		{
			return data.ID;
		}
		
		public function updateAttr():void
		{
			data.MAXHP = data.Vitality * 4 + data.Strength + data.Toughness + data.Dexterity;
		}
		
		
		
		////////////////////////////////////////////////////////////////////////////////////
		// move 
		
		public function setDirect( dir:int ):void
		{
			moveHandler.moveDirection = dir;
		}
		
		public function Direction():int
		{
			return moveHandler.moveDirection;
		}
		
		public function endMoving():void
		{
			moveHandler.endMoving();
		}
		
		public function startMove( x:int , y:int ):void
		{
			
			if ( state == GameCeatureState.MOVE ) 
			{
				//if ( x == moveHandler.mapGridLast.x && y == moveHandler.mapGridLast.y ) 
				//{
					// too fast
				//	return;
				//}
				
				moveHandler.movePathBuffer = true;
				moveHandler.movePathBackup.setValue( x , y );
				
				return;
			}
			
			
			moveHandler.movePath = PathFinder.instance.findPath( moveHandler.mapPos.mapGrid.x , moveHandler.mapPos.mapGrid.y , x , y ).concat();
			//trace( moveHandler.movePath );
			moveHandler.sendMoveMsg();
			moveHandler.movePath.shift();
			
			startMoving();
		}
		
		public function startMoving():void
		{
			moveHandler.startMoving();
			
			if ( team && team.leader == this )
			{
				for ( var i:int = 1 ; i < team.members.length ; i++ )
				{
					team.members[i].moveHandler.movePath = team.members[i-1].moveHandler.movePath.concat();
					
					team.members[i].moveHandler.movePath.unshift( team.members[i-1].moveHandler.movePos.clone() );
					team.members[i].moveHandler.movePath.pop();
					team.members[i].moveHandler.movePath.unshift( team.members[i-1].moveHandler.mapPos.mapGrid.clone() );
					
					team.members[i].moveHandler.startMoving();
					
					//trace( moveHandler.movePath );
					trace( team.members[i].moveHandler.movePath );
				}
			}
		}
		
		public function set MovePathBuffer( b:Boolean ):void
		{
			moveHandler.movePathBuffer = b;
		}
		public function get MovePathBuffer():Boolean
		{
			return moveHandler.movePathBuffer;
		}
		
		public function set MovePathBackup( grid:MapGrid ):void
		{
			moveHandler.movePathBackup = grid;
		}
		public function get MovePathBackup():MapGrid
		{
			return moveHandler.movePathBackup;
		}
		
		public function set MovePath( a:Array ):void
		{
			moveHandler.movePath = a;
		}
		public function get MovePath():Array
		{
			return moveHandler.movePath;
		}
		
		public function set MapID( id:int ):void
		{
			moveHandler.mapPos.mapID = id;
		}
		public function get MapID():int
		{
			return moveHandler.mapPos.mapID;
		}
		
		public function set MMapGrid( id:MapGrid ):void
		{
			moveHandler.mapPos.mapGrid = id;
		}
		public function get MMapGrid():MapGrid
		{
			return moveHandler.mapPos.mapGrid;
		}
		
		public function setPos( x:int , y:int ):void
		{
			moveHandler.mapPos.mapGrid.setValue( x , y );
			
		}
		
		public function move():void
		{
			if ( mainPlayer ) 
			{
				GameScene.instance.move( moveHandler.mapPos.mapGrid );
				MapUIHandler.instance.updatePos();
			}
		}
		
		////////////////////////////////////////////////////////////////////////////////////
		// action
		
		public function get ActionTextureID():int
		{
			return actionHandler.textureID;
		}
		
		
		public function updateActionID():void
		{
			if ( data.Index != GameDefine.INVALIDID && data.Index != 0 ) 
			{
				actionHandler.setID( data.Index );
			}
			else
			{
				data.Index = RoleConfig.instance.getIndex( data.Role , data.Color , GameDefine.INVALIDID );
				actionHandler.setID( data.Index );
			}
			
		}
		
		
		public function setAction( id:int ):void
		{
			actionHandler.setAction( id , moveHandler.moveDirection );
		}
		
		public function sendActionMsg( id:int ):void
		{
			actionHandler.sendActionMsg( id );
		}
		
		public function addCommomAction( id:int , actionID:int , stop:Boolean ):void
		{
			var a:GameCommonAction = new GameCommonAction();
			a.init( this );
			a.setID( id );
			a.setActionID( actionID , stop );
			
			actionArray.push( a );
		}
		
		public function CommomActionTextureID( id:int ):int
		{
			for ( var i:int = 0 ; i < actionArray.length ; i++ )
			{
				if ( actionArray[i].id == id )
				{
					return actionArray[i].textureID;
				}
			}
			
			return GameDefine.INVALIDID;
		}
		
		public function removeCommomAction( id:int ):void
		{
			for ( var i:int = 0 ; i < actionArray.length ; i++ )
			{
				if ( actionArray[i].id == id )
				{
					actionArray.splice( i , 1 );
					return;
				}
			}
		}
		
		public function setCommomActionID( id:int , actionID:int , stop:Boolean ):void
		{
			for ( var i:int = 0 ; i < actionArray.length ; i++ )
			{
				if ( actionArray[i].id == id )
				{
					actionArray[i].setActionID( actionID , stop );
					return;
				}
			}
		}
		
		////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		
		public function update( delay:Number ):void
		{
			moveHandler.update( delay );
			actionHandler.update( delay );
			
			for ( var i:int = 0 ; i < actionArray.length ; i++ )
			{
				actionArray[i].update( delay );
			}
		}

		
		public function init():void
		{
			data = new GameCreatureData();
			
			moveHandler = new GameCreatureMove();
			moveHandler.init( this );
			
			//moveBuffer = new Array();
			
			actionHandler = new GameCreatureAction();
			actionHandler.init();
			
			actionArray = new Array();
			
			addCommomAction( 25600 , 0 , false );
		}
		
		public function release():void
		{
			data = null;
			
			moveHandler.release();
			moveHandler = null;
			
			actionHandler.release();
			actionHandler = null;
			
			while( actionArray.length )
			{
				actionArray[0].release();
				actionArray[0] = null;
				actionArray.shift();
			}
			actionArray = null;
		}
		
		public function GameCreature() 
		{
			
		}
		
		
	}
	
}